//
//  WAGoodDetailController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/16.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WAGoodDetailController.h"
#import "WACycleScrollView.h"
#import "WAEnsureTransactionController.h"
#import "WAGoodModel.h"
#import "WACustomImageVIew.h"

@interface WAGoodDetailController ()
@property (nonatomic, strong) WAGoodModel *goodModel;
@property (nonatomic, strong) NSString *goodID;
@property (weak, nonatomic) IBOutlet UILabel *titleLB;
@property (weak, nonatomic) IBOutlet UIView *contentView;

//中间LB
@property (weak, nonatomic) IBOutlet UILabel *ylCountLB;
@property (weak, nonatomic) IBOutlet UILabel *kucunLB;
@property (weak, nonatomic) IBOutlet UILabel *zengsongLB;
@property (weak, nonatomic) IBOutlet UILabel *goodDetailLB;

//四张图片
@property (weak, nonatomic) IBOutlet UIImageView *topImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topImageViewHeightCons;

@property (weak, nonatomic) IBOutlet UIImageView *secordImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *secordImageViewHeightCons;

@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation WAGoodDetailController

- (instancetype)initWithID:(NSString *)goodID
{
    self = [super init];
    if (self) {
        self.goodID = goodID;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self requestData];
}

- (void)requestData{
    [WNTool showLoading];
    [WNBusinessRequest getSinglGoos:self.goodID Succ:^(id  _Nonnull responseObject) {
        [WNTool hideLoading];
        NSDictionary *dict = (NSDictionary *)responseObject;
        self.goodModel = [WAGoodModel mj_objectWithKeyValues:dict];
//        self.goodModel.time = [NSString stringWithFormat:@"%@;%@;%@;%@;%@;%@;%@;%@;%@;%@;%@;%@;%@",self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1,
//                               self.goodModel.image1];
        [self upddateViews];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [WNTool show:mes];
    }];
}

- (void)upddateViews{
    

    
    
    self.kucunLB.text = [NSString stringWithFormat:@"库存：%zd",self.goodModel.stocks];
    self.zengsongLB.text = [NSString stringWithFormat:@"赠送雨露：%.1f",self.goodModel.yulunumber];
    self.ylCountLB.text = [NSString stringWithFormat:@"雨露：%.1f",self.goodModel.yuluprice];
//    self.goodDetailLB.text = self.goodModel.marks;
    
    self.goodDetailLB.attributedText = [self commentWithString:self.goodModel.marks];
    
    [self sacleImageView:self.topImageView
                imageUrl:self.goodModel.image0
                    cons:self.topImageViewHeightCons];
    
    [self sacleImageView:self.secordImageView
                imageUrl:self.goodModel.image1
                    cons:self.secordImageViewHeightCons];
    
    
    UIView *view = self.secordImageView;
    NSArray *imageList = [self.goodModel.time componentsSeparatedByString:@";"];
    for (NSString * imageurl in imageList) {
        if(![imageurl hasPrefix:@"http://"]){
            continue;
        }
        UIImageView *imageView = [UIImageView new];
        
        [imageView sd_setShowActivityIndicatorView:YES];
        [imageView sd_setIndicatorStyle:UIActivityIndicatorViewStyleMedium];
        [self.contentView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_bottom);
            make.left.right.equalTo(self.contentView);
            make.height.mas_equalTo(@(50));
        }];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageurl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            CGFloat imageScale = image.size.height / image.size.width;
            [imageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@(WNScreenWidth * imageScale));
            }];
        }];
        view = imageView;
    }
    
    
    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom);
    }];
}


- (void)sacleImageView:(UIImageView *)imageView
              imageUrl:(NSString *)imageUrl
                  cons:(NSLayoutConstraint *)cons{
    cons.constant = 40;
    if ([imageUrl hasPrefix:@"http"]) {
        [imageView sd_setShowActivityIndicatorView:YES];
        [imageView sd_setIndicatorStyle:UIActivityIndicatorViewStyleMedium];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            CGFloat imageScale = image.size.height / image.size.width;
            cons.constant = WNScreenWidth * imageScale;
        }];
    }else{
        cons.constant = 0;
    }
}

- (IBAction)bugAction:(id)sender {
    WAEnsureTransactionController *VC = [[WAEnsureTransactionController alloc] initWithID:self.goodID];
    [self.navigationController pushViewController:VC animated:YES];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//评论排版
- (NSAttributedString *)commentWithString:(NSString *)string{
    if (!string) {
        string = @"";
    }
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:string];
    //字体大小
    [attributeString addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:16]
                            range:NSMakeRange(0, string.length)];
    //字体颜色
    [attributeString addAttribute:NSForegroundColorAttributeName
                            value:[UIColor blackColor]
                            range:NSMakeRange(0, string.length)];
    
    //段落
    NSMutableParagraphStyle   *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5;
    [attributeString addAttribute:NSParagraphStyleAttributeName
                            value:paragraphStyle
                            range:NSMakeRange(0, string.length)];//设置段落样式
    return [attributeString copy];
}

@end
